import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import router from 'umi/router';

import _ from 'lodash';
import { fetchValidation } from '@/utils';

import {
  queryPosthouseContractDetail,
  queryFranchiseeList,
  queryContractPartyList,
  savePosthouseContract,
  queryPosthouseList,
  queryGenerateNum,
} from './service';


export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T }
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchFranchisees: Effect;
    fetchContractPartys: Effect;
    fetchPosthouse: Effect;
    generateNum: Effect;
    save: Effect;
  };
  reducers: {
    show: Reducer;
    franchisees: Reducer;
    clean: Reducer;
    contractpartys: Reducer;
    signingStations: Reducer;
    setGenerateNum: Reducer;
  };
}

const defaultData = {
  detail: {},
  franchiseesList: [],
  contractpartyslist: [],
  signingStationList: [],
  generate: '',
};

const Model: ModelType = {
  namespace: 'posthouseContractAdd',

  state: defaultData,

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryPosthouseContractDetail, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'show',
        payload: response.body,
      });
    },
    * fetchFranchisees({ payload = {} }, { call, put }) {
      payload = {
        ...payload,
        pageNum: 1,
        numPerPage: 20000,
      };
      const response = yield call(queryFranchiseeList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'franchisees',
        payload: response.body,
      });
    },
    * fetchContractPartys({ payload }, { call, put }) {
      const response = yield call(queryContractPartyList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'contractpartys',
        payload: response.body,
      });
    },
    * save({ payload }, { call, put }) {
      const response = yield call(savePosthouseContract, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('提交成功');
        yield put({
          type: 'global/setPath',
        });
        router.push('/partner/posthouseContract/list');
      }
    },
    * fetchPosthouse({ payload }, { call, put }) {
      const response = yield call(queryPosthouseList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'signingStations',
        payload: response.body,
      });
    },
    * generateNum({ payload }, { call, put }) {
      const response = yield call(queryGenerateNum, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'setGenerateNum',
        payload: response.body.contract_num || '',
      });
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        detail: payload,
      };
    },
    franchisees(state, { payload }) {
      return {
        ...state,
        franchiseesList: payload,
      };
    },
    contractpartys(state, { payload }) {
      return {
        ...state,
        contractpartyslist: payload,
      };
    },
    signingStations(state, { payload }) {
      return {
        ...state,
        signingStationList: payload,
      };
    },
    setGenerateNum(state, { payload }) {
      return {
        ...state,
        generate: payload,
      };
    },
    clean() {
      return defaultData;
    },
  },
};

export default Model;
